#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char* argv[]) {
	char* buff;
	buff = malloc(1024);
	char* token;
	int status = 0;
	int length;
	char cwd[1024];
	char* copy;
	copy = malloc(1024);
	char* pass;
	pass = malloc(1024);
	char* path;
	path = malloc(1024);
	int pid;
	path = getenv("PATH"); //gets the path environment variable

	while (1) {
		printf("%% ");
		scanf("%[^\n]%*c", buff);
		length = strlen(buff);

		if (strcmp(buff, "dir") == 0) { //displays current working directory
			getcwd(cwd, sizeof(cwd));
			fprintf(stdout, "Current working dir: %s\n", cwd);
		}

		if (strcmp(buff, "exit") == 0) {
			exit(0);
		}
		//---------------------------------------------------------displays path
		else if (strcmp(buff, "path") == 0) {
			//displays path name currently set; ie. /home/bin
			//displays current working directory
			if (getcwd(cwd, sizeof(cwd)) != NULL) {
				fprintf(stdout, "Current working dir: %s\n", cwd);
			}
			else
				perror("getcwd() error");

		}
		//---------------------------------------------------------begin change directory
		else if (length > 4) {
			token = strtok(buff, " "); //gets first token of the string, puts it in token
			if(strcmp(token, "cd") == 0) {
				while(token != NULL) {
					token = strtok(NULL, " ");
					if(token != NULL) {
                        strcat(pass, token); //puts remaining part of string after cd into pass var
                    }
                }
                printf("This will change the directory.\n");

                        if(chdir(pass) == -1) { //changes the directory
                        	printf("chdir error\n");
                        }
                        else {
                        	printf("Directory changed: \n");
                        	if (getcwd(cwd, sizeof(cwd)) != NULL) {
                        		fprintf(stdout, "Current working dir: %s\n", cwd);
                                        strcpy(pass,""); //empty the passing string
                                    }
                                    else {
                                    	perror("getcwd() error");
                                    }
                                }
                            }
			//----------------------------------------------------------end change directory
			//----------------------------------------------------------begin path append/remove
                            else if (strcmp(token, "path") == 0) {
				//check for plus or minus sign; will be space, + or -, space, then pathname
				token = strtok(NULL, " "); // gets the next token, either + or -
				
				if (strcmp(token, "+") == 0) {
					token = strtok(NULL, " ");
					printf("For +, the token is %s\n", token);

					if (token != NULL) { //if there is a value..
						strcat(pass, token); //puts remaining part of string after cd into pass var
						//printf("this is new path: %s\n",pass);
					}

				if(strstr(cwd,pass)){ // see if entered path is already in cwd or if its null
					printf("Appending failed - %s is already in currrent dir or is null.\n",pass);
				}	

				else {
					getcwd(cwd, sizeof(cwd)); 

					strcat(cwd,pass); // append entered path to cwd
					chdir(cwd); // change directory with appended path
					
					printf("Directory changed.. \n");
					if (getcwd(cwd, sizeof(cwd)) != NULL) { //as long as current working directory
						fprintf(stdout, "Current working dir: %s\n", cwd);
						strcpy(pass, ""); //empty the passing string
					} else {
						printf ("Appending failed - %s\n", strerror (errno));
					}
				}

			} 

			else if (strcmp(token, "-") == 0) {
				token = strtok(NULL, " ");
				printf("For - the token is %s\n", token);

				if (token != NULL) { //if there is a value..
					strcat(pass, token); //puts remaining part of string after cd into pass var
					//printf("this is new path: %s\n",pass);
				}
				getcwd(cwd, sizeof(cwd)); 


				//--Under Construction

				//see if string pass is in cwd
				//find the string in pass
				//remove occurance of pass in cwd

				if(strstr(cwd,pass)){
					//int index = strcspn(cwd,pass);
					/*
					char *index = strstr(cwd,pass);
					int in = int(index - cwd);
					printf("pass is at %i\n",in);
					*/
               
               
				  /* int count1 = 0, count2 = 0, i, j, flag;
					char search[100];
					while (cwd[count1]!= '�')
						count1++;
					while (pass[count2]!='�')
						count2++;

					for (i=0;i<=count1-count2;i++){
						for(j=i;j<i+count2;j++){
							flag=1;
							if(cwd[j]!= search[j-i]){
								flag=0;
								break;
							}
						}
						if(flag==1)
							break;
					}
					if (flag==1)
						printf("found path to delete");
					else
						printf("nvm");*/
				}
				else
					printf("path not in cwd");

				

				
				getcwd(cwd, sizeof(cwd)); 

					chdir(cwd); // change directory with appended path
					
					printf("Directory changed.. \n");
					if (getcwd(cwd, sizeof(cwd)) != NULL) { //as long as current working directory
						fprintf(stdout, "Current working dir: %s\n", cwd);
						strcpy(pass, ""); //empty the passing string
					} else {
						printf ("Appending failed - %s\n", strerror (errno));
					}
					
				}
			}

			//else if() { //todo; for pipes?

			//}

			else {
				printf("Command undefined.\n");
			}
		}

		else {
			printf("Command undefined.\n");
		}

	} //end while

	exit(0);
} //end main

